#
#	El manager v.0.01 - Derivart (http://www.derivart.info)
#	@Coded: Mar Cane / Martin Nadal (2009)
#
from subprocess import Popen,PIPE
import os,re
import urllib
from BeautifulSoup import BeautifulSoup
from song import song
from osDetector import osDetector

# key of application or developer to use MusicIp
class genPuid():
    def __init__(self):
        self.puidkey = "86770130f70cb4346976c260b25d5822" 
        self.myOS = osDetector()

    def checkPuid(self, path):
        if(myOS.getOSPlatform()=="mac"):
            checkPuid_OSX(path)
        elif(myOS.getOSPlatform()=="Linux"):
            checkPuid_LINUX(path)
        elif(myOS.getOSPlatform()=="Windows"):
            checkPuid_WIN(path)

    def extractMetadata(self,xml):
        dom = BeautifulSoup(xml)
        artist = dom.find('name')
        title = dom.find('title')
        print "genpuid-"+str(artist)
        print "genpuid-"+str(title)
        return song( path, title, artist)

    def checkPuid_OSX(self, path):
        cmd = './genpuid-1.4-mac/genpuid '+puidkey + ' -rmd=1 '
        p = Popen(cmd+re.escape(path),shell=True,stdin=PIPE,stdout=PIPE)
        xml = p.communicate()[0]
        return self.extractMetadata(xml)

    def checkPuid_WIN(self, path):
		cmd = './genpuid-1.4-win/genpuid '+puidkey + ' -rmd=1 '
		p = Popen(cmd+re.escape(path),shell=True,stdin=PIPE,stdout=PIPE)
		xml = p.communicate()[0]
		return self.extractMetadata(xml)
    def checkPuid_LINUX(self, path):
		cmd = './genpuid-1.4-linux/genpuid '+puidkey + ' -rmd=1 '
		p = Popen(cmd+re.escape(path),shell=True,stdin=PIPE,stdout=PIPE)
		xml = p.communicate()[0]
		return self.extractMetadata(xml)

#gen = genPuid()
#gen.checkPuid() 